package com.wn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.bean.Building;
import com.wn.bean.Constants;
import com.wn.bean.PageBean;
import com.wn.bean.R;
import com.wn.service.IBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellow Docter
 * @since 2022-05-19
 */
@RestController
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    private IBuildingService buildingService;

    /**
     *
     *  服务器的分页操作步骤:
     *
     *  01 配置分页插件
     *
     *     @Bean
     *     public MybatisPlusInterceptor mybatisPlusInterceptor() {
     *         MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
     *         // 切换成你自己的数据库
     *         interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
     *         return interceptor;
     *     }
     *
     *  02 拿到分页的参数
     *       queryBuild(Integer currentPage,Integer pageSize)
     *
     *  03 创建 ipage 对象
     *       IPage<Building> page = new Page<>(currentPage,pageSize);
     *
     *  04 调用分页方法
     *        IPage<Building> iPage = buildingService.page(page);
     *
     *  05 返回数据
     *     1. 组装对象
     *     2. 返回结果
     * @return
     */
    @GetMapping
    public R queryBuild(Integer currentPage,Integer pageSize){

        //创建分页对象
        IPage<Building> page = new Page<>(currentPage,pageSize);

        IPage<Building> iPage = buildingService.page(page);

        //分页对象记录列表
        List<Building> records = iPage.getRecords();

        // 总数
        Long total = iPage.getTotal();

        // 分页数据的封装对象
        PageBean<Building> pageBean = new PageBean<>();
        pageBean.setData(records);
        pageBean.setTotal(total.intValue());

        if (!CollectionUtils.isEmpty(records)){

            R r = new R();
            r.setData(pageBean);
            r.setMsg("成功");
            r.setCode(Constants.SUCCESS_CODE);
            return  r;
        }
        return R.ERROR("数据为空");

    }

    @PutMapping
    public R updateBuild(@RequestBody Building building){

        boolean b = buildingService.updateById(building);

        if (b){
            return R.OK();
        }
        return R.ERROR();
    }

    @DeleteMapping
    public R deleteBuilds(@RequestBody List<Integer> ids){

        System.out.println("ids = " + ids);
        boolean b = buildingService.removeBatchByIds(ids);

        if (b){
            return R.OK();
        }
        return R.ERROR();
    }

    @GetMapping("/all")
    public R queryBuildAll(){

        List<Building> list  = buildingService.list();

        if (!CollectionUtils.isEmpty(list)){

            R r = new R();
            r.setData(list);
            r.setMsg("成功");
            r.setCode(Constants.SUCCESS_CODE);
            return  r;
        }
        return R.ERROR("查询失败");
    }

    @DeleteMapping("/byid/{id}")
    public R deleteBuildById(@PathVariable Integer id){

        System.out.println("id = " + id);
        boolean b = buildingService.removeById(id);

        if (b){
            return R.OK();
        }
        return R.ERROR();
    }

}
